Mastering Scala: Challenging Assignments and Expert Solutions
Welcome, eager learners, to our realm of Scala mastery! At programminghomeworkhelp.com, we understand the trials and tribulations of grappling with Scala assignments. Fear not, for our team of seasoned experts is here to guide you through the intricacies of this powerful language.
In this post, we present a couple of master-level Scala questions that will put your skills to the test. But fret not, for accompanying each question is a detailed solution crafted by our esteemed Scala assignment helper. So, let's dive into the depths of Scala brilliance!
Question 1: The Power of Pattern Matching
Consider the following scenario: You are tasked with implementing a function that calculates the factorial of a given integer using pattern matching in Scala. Your function should handle both positive and negative integers. Additionally, it should gracefully handle edge cases such as zero.
Solution 1
```scala
object FactorialCalculator {
def factorial(n: Int): Int = n match {
case 0 | 1 => 1
case _ if n < 0 => throw new IllegalArgumentException("Factorial is not defined for negative numbers")
case _ => n * factorial(n - 1)
}
object FactorialCalculator {
def factorial(n: Int): Int = n match {
case 0 | 1 => 1
case _ if n < 0 => throw new IllegalArgumentException("Factorial is not defined for negative numbers")
case _ => n * factorial(n - 1)
}
def main(args: Array[String]): Unit = {
println(factorial(5)) // Output: 120
println(factorial(-3)) // Output: Exception - Factorial is not defined for negative numbers
}
}
```
println(factorial(5)) // Output: 120
println(factorial(-3)) // Output: Exception - Factorial is not defined for negative numbers
}
}
```
In this solution, we utilize pattern matching to elegantly handle different cases. For 0 and 1, the factorial is trivially 1. For negative numbers, an exception is thrown as factorial is not defined for them. Otherwise, we recursively calculate the factorial.
Question 2: Embracing Higher-Order Functions
Now, let's delve into the realm of higher-order functions. Your task is to implement a function `applyNTimes` that takes a function `f: T => T` and an integer `n`, and returns another function that applies `f` to its argument `n` times.
Solution 2
```scala
object HigherOrderFunctions {
def applyNTimes[T](f: T => T, n: Int): T => T = {
require(n >= 0, "Number of times must be non-negative")
object HigherOrderFunctions {
def applyNTimes[T](f: T => T, n: Int): T => T = {
require(n >= 0, "Number of times must be non-negative")
def applyOnce(x: T): T = {
if (n == 0) x
else applyNTimes(f, n - 1)(f(x))
}
if (n == 0) x
else applyNTimes(f, n - 1)(f(x))
}
applyOnce _
}
}
def main(args: Array[String]): Unit = {
val addOne = (x: Int) => x + 1
val addTenTimes = applyNTimes(addOne, 10)
println(addTenTimes(5)) // Output: 15
}
}
```
val addOne = (x: Int) => x + 1
val addTenTimes = applyNTimes(addOne, 10)
println(addTenTimes(5)) // Output: 15
}
}
```
In this solution, we define the `applyNTimes` function using recursion. We first ensure that `n` is non-negative. Then, we define an inner function `applyOnce` that applies `f` to its argument `n` times recursively. Finally, we return `applyOnce` as the result.
With these challenging questions and comprehensive solutions, we hope to ignite your passion for Scala and empower you to tackle even the most formidable assignments with confidence. Remember, at programminghomeworkhelp.com, we're here to serve as your trusted guides on your journey to Scala mastery.